/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: core_error_domain.h
 *
 * Purpose: This section describes the ara::core::CoreErrorDomain type that 
 *          derives from ara::core::ErrorDomain and contains the errors that 
 *          can originate from within the CORE Functional Cluster
 *
 * Developer:
 *   wen.gu , 2020-04-08
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_CORE_CORE_ERROR_DOMAIN_H__
#define __ARA_CORE_CORE_ERROR_DOMAIN_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#include "ara/core/error_domain.h"
#include "ara/core/exception.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace core
{

enum class CoreErrc : ErrorDomain::CodeType
{
    kInvalidArgument = 22, /** an invalid argument was passed to a function */
    kInvalidMetaModelShortname = 137, /** given string is not a valid model element shortname */
    kInvalidMetaModelPath = 138, /** missing or invalid path to model element */
};

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

//class ErrorCode;

class CoreException : public Exception
{
public:
    explicit CoreException(ErrorCode err) noexcept
        :Exception(err)
    {
        /** todo something */
    }
};


class CoreErrorDomain final : public ErrorDomain
{
    using Errc = CoreErrc;
    using Exception = CoreException;
public:
    constexpr CoreErrorDomain(void) noexcept
        :ErrorDomain(0x8000000000000014ULL)
    {
        /** todo something */
    }

    ~CoreErrorDomain() = default;
public:
    char const* Name() const noexcept override { return "Core"; }
    char const* Message(ErrorDomain::CodeType errorCode) const noexcept override
    {
        char const* ret = "";
        Errc err = static_cast<Errc>(errorCode);
        switch (err)
        {
            ERR_DOMAIN_CASE(Errc::kInvalidArgument);
            ERR_DOMAIN_CASE(Errc::kInvalidMetaModelPath);
            ERR_DOMAIN_CASE(Errc::kInvalidMetaModelShortname);
        default: break;
        }

        return ret;
    }
    void ThrowAsException(ErrorCode const& errorCode) const noexcept(false) override
    {
        throw CoreException(errorCode);
    }

public:
    static CoreErrorDomain const& GetInstance()
    {
        static CoreErrorDomain cem;
        return cem;
    }

    constexpr CoreErrorDomain const& GetRef() const { return  *this; }

};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

constexpr inline ErrorDomain const& GetCoreErrorDomain() noexcept
{
    return CoreErrorDomain::GetInstance().GetRef();
}

constexpr inline ErrorCode MakeErrorCode(CoreErrc code, ErrorDomain::SupportDataType data) noexcept
{
    return std::move(ErrorCode(static_cast<ErrorDomain::CodeType>(code), GetCoreErrorDomain(), data));
}

} /** namespace core */
} /** namespace ara */
#endif /** !__ARA_CORE_CORE_ERROR_DOMAIN_H__ */

